const {
  appid,
  secret,
  accessToken,
  dbAccessToken
} = require("./config.js");
const db = uniCloud.database();
module.exports = {
  _before: async function() { // 通用预处理器
    // this.accessToken = await accessToken();
    this.accessToken = await dbAccessToken();
  },
  //获取有数量限制的小程序码
  async getwxacode({
    path = "pages/index/index",
    width = 430,
    auto_color = false,
    line_color = {
      "r": 0,
      "g": 0,
      "b": 0
    },
    is_hyaline = false,
    env_version = "release"
  } = {}) {
    let {
      data
    } = await uniCloud.request({
      url: "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + this.accessToken,
      method: "POST",
      data: {
        path,
        width,
        auto_color,
        line_color,
        is_hyaline,
        env_version
      },
      responseType: "buffer"
    })
    return "data:image/png;base64," + data.toString('base64')
  },


  //获取不限制数量的小程序码
  async getUnlimited({
    scene = "0",
    page = "pages/index/index",
    check_path = true,
    env_version = "release",
    width = 430,
    auto_color = false,
    line_color = {
      "r": 0,
      "g": 0,
      "b": 0
    },
    is_hyaline = false
  } = {}) {
    let {
      data
    } = await uniCloud.request({
      url: "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + this.accessToken,
      method: "POST",
      data: {
        scene,
        page,
        check_path,
        env_version,
        width,
        auto_color,
        line_color,
        is_hyaline
      },
      responseType: "buffer"
    })
    return "data:image/png;base64," + data.toString('base64')

  },

  //获取有数量限制的二维码
  async createwxaqrcode({
    path = "pages/index/index",
    width = 430
  } = {}) {
    let {
      data
    } = await uniCloud.request({
      url: "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + this.accessToken,
      method: "POST",
      data: {
        path,
        width
      },
      responseType: "buffer"
    })
    return "data:image/png;base64," + data.toString('base64')
  }
}